<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\User;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;

class ReviewVipExpiry extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cc:review-vip';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        User::query()->whereNotnull('vip_expired_at')->where('vip_expired_at',"<",Carbon::now())->chunkById(100, function (Collection $users) {
                
                if ($users->isEmpty()) return false;
                
                foreach ($users as $user) {

                    $user->update(['grade' => 'normal']);
                    if($user->parent){

                        user_upgrade($user->parent);
                    }
                }
        });
    }
}
